package vn.edu.ptithcm.pim.controller;

import java.io.File;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StringMultipartFileEditor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import vn.edu.ptithcm.pim.common.bean.Attachment;
import vn.edu.ptithcm.pim.common.bean.SendMailObject;
import vn.edu.ptithcm.pim.common.bean.TranslatorObject;
import vn.edu.ptithcm.pim.constant.Constants;
import vn.edu.ptithcm.pim.constant.Locales;
import vn.edu.ptithcm.pim.constant.TaskStatuses;
import vn.edu.ptithcm.pim.constant.UrlConstants;
import vn.edu.ptithcm.pim.dom.ActivityLog;
import vn.edu.ptithcm.pim.dom.Employee;
import vn.edu.ptithcm.pim.dom.Project;
import vn.edu.ptithcm.pim.dom.Resource;
import vn.edu.ptithcm.pim.dom.Task;
import vn.edu.ptithcm.pim.dom.TaskLog;
import vn.edu.ptithcm.pim.dom.User;
import vn.edu.ptithcm.pim.service.ActivityLogService;
import vn.edu.ptithcm.pim.service.EmployeeFunctionInProjectService;
import vn.edu.ptithcm.pim.service.EmployeeService;
import vn.edu.ptithcm.pim.service.PhaseService;
import vn.edu.ptithcm.pim.service.ProjectService;
import vn.edu.ptithcm.pim.service.ResourceService;
import vn.edu.ptithcm.pim.service.SendMailService;
import vn.edu.ptithcm.pim.service.TaskCategoryService;
import vn.edu.ptithcm.pim.service.TaskLogService;
import vn.edu.ptithcm.pim.service.TaskPriorityService;
import vn.edu.ptithcm.pim.service.TaskService;
import vn.edu.ptithcm.pim.service.TaskSeverityService;
import vn.edu.ptithcm.pim.service.TaskStatusService;
import vn.edu.ptithcm.pim.service.UserService;
import vn.edu.ptithcm.pim.util.DateTimeUtil;
import vn.edu.ptithcm.pim.util.DocumentUtil;
import vn.edu.ptithcm.pim.util.FileUtil;
import vn.edu.ptithcm.pim.util.ParamUtil;
import vn.edu.ptithcm.pim.util.TaskStatusUtil;

@SuppressWarnings("deprecation")
public class UpdateTaskController extends SimpleFormController {
	private TaskService taskService;
	private UserService userService;
	private TaskSeverityService taskSeverityService;
	private TaskCategoryService taskCategoryService;
	private TaskPriorityService taskPriorityService;
	private TaskStatusService taskStatusService;
	private ProjectService projectService;
	private EmployeeFunctionInProjectService employeeFunctionInProjectService;
	private EmployeeService employeeService;
	private TaskLogService taskLogService;
	private PhaseService phaseService;
	private SendMailService sendMailService;
	private LocaleResolver localeResolver;
	private ReloadableResourceBundleMessageSource messageSource;
	private ResourceService resourceService;
	private ActivityLogService activityLogService;

	Logger log = Logger.getLogger(UpdateTaskController.class);

	public UpdateTaskController() {
		super();
		setSessionForm(true);
		setCommandName("task");
		setCommandClass(Task.class);
		setFormView(UrlConstants.UPDATE_TASK_URL);
		setSuccessView(UrlConstants.UPDATE_TASK_URL);
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		Task task = null;
		String tcode = ParamUtil.getString(request, "tcode", "");
		task = taskService.findByCode(tcode);
		if (task == null) {
			task = new Task();
		}
		return task;
	}

	@Override
	public void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
		if (request instanceof MultipartHttpServletRequest) {
			binder.registerCustomEditor(String.class,
					new StringMultipartFileEditor());
		}
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		Task informTask = (Task) command;
		String loginName = userService.getCurrentLogin(request);
		User user = userService.findByLoginName(loginName);
		String visa = user.getEmployee().getVisa();

		if (user != null) {
			int pnum = ParamUtil.getInteger(request, "pnum", 0);
			Project project = projectService.findByNumber(pnum);
			if (project != null) {

				Long taskId = informTask.getTaskid();
				Task inDBTask = taskService.findById(taskId);

				if (inDBTask != null) {
					int errorCode = 0;
					boolean checkUpdate = true;

					if (informTask.getTaskStatus().getStatusid() >= TaskStatuses.TOBE_REVIEWED) {
						if (!checkChildRecursion(inDBTask, informTask
								.getTaskStatus().getStatusid())) {
							checkUpdate = false;
							errorCode = 2;
						}
					}

					if (!checkParent(inDBTask, informTask.getTaskStatus()
							.getStatusid())) {
						checkUpdate = false;
						errorCode = 1;
					}

					if (checkUpdate) {
						log.info("in form version:" + informTask.getVersion());
						Long version = new Long(0);
						Date now = new Date();

						version = inDBTask.getVersion();
						log.info("in db version:" + version);

						if (!version.equals(informTask.getVersion())) {
							request.setAttribute("isOptimisticLocking", true);

						} else {

							List<Attachment> attachments = new ArrayList<Attachment>(
									0);

							String attachedfiles = ParamUtil.getString(request,
									"_attachedfiles", "");
							if (!attachedfiles.isEmpty()) {
								String[] files = attachedfiles.split(";");
								if (files.length > 0) {
									informTask.setAttachedfiles(true);
									String path = request.getSession()
											.getServletContext()
											.getRealPath("");
									int index = StringUtils
											.indexOf(path, "pim");
									path = path.substring(0, index);

									for (int i = 0; i < files.length; ++i) {
										String[] fileInfo = files[i].split(",");
										if (fileInfo.length == 2) {
											Resource resource = new Resource();
											resource.setAuthor(visa);
											resource.setCreatedAt(now);
											resource.setOrignalName(DocumentUtil
													.getFileName(fileInfo[0]));
											resource.setPath(fileInfo[1]);
											resource.setSubjectId(inDBTask
													.getTaskid());
											resource.setSubjectType("Task");
											resource.setType(DocumentUtil
													.getFileExtension(fileInfo[0]));
											resource.setSize(FileUtil
													.getSizeInKByte(path
															+ fileInfo[1]));

											FileNameMap fileNameMap = URLConnection
													.getFileNameMap();

											File file = FileUtils.getFile(path
													+ fileInfo[1]);

											String mimeType = fileNameMap
													.getContentTypeFor(file
															.getName());

											Attachment attachment = new Attachment(
													FileUtils
															.readFileToByteArray(file),
													DocumentUtil
															.getFileName(fileInfo[0]),
													mimeType, true);
											attachments.add(attachment);

											resourceService.save(resource);
										}
									}
								}
							}

							informTask.setLastchanged(now);

							List<TranslatorObject> changedProperties = getChangedProperties(
									request, inDBTask, informTask);

							for (TranslatorObject translatorObject : changedProperties) {

								TaskLog taskLog = new TaskLog();
								taskLog.setTask(inDBTask);
								taskLog.setTime(now);
								taskLog.setField(translatorObject.getKey());
								taskLog.setVisa(visa);
								taskLog.setChange(translatorObject
										.getDescription());
								taskLogService.save(taskLog);
							}

							taskService.saveOrUpdate(informTask);

							if (changedProperties.size() > 0) {
								Task task = taskService.findById(informTask
										.getTaskid());

								SendMailObject sendMailObject = new SendMailObject();
								StringBuilder subject = new StringBuilder();
								subject.append("[")
										.append(project.getName())
										.append(" - ")
										.append(task.getTaskCategory()
												.getName()).append(" #")
										.append(task.getCode()).append("]")
										.append(" (")
										.append(task.getTaskStatus().getName())
										.append(") ").append(task.getSummary());

								sendMailObject.setSuject(subject.toString());
								sendMailObject
										.setFrom(Constants.MAIL_HOST_SERVER);

								Map<String, Object> data = new HashMap<String, Object>();
								data.put("project", project);
								data.put("task", task);
								data.put("starttime", DateTimeUtil
										.dateToString(task.getStarttime(),
												"yyyy-MM-dd"));
								data.put("endtime", DateTimeUtil.dateToString(
										task.getEndtime(), "yyyy-MM-dd"));
								data.put("visa", visa);

								String taskDetailUrl = request
										.getRequestURL()
										.toString()
										.replaceAll(request.getServletPath(),
												"/taskDetail.htm")
										+ "?pnum="
										+ pnum
										+ "&tcode="
										+ task.getCode();

								data.put("taskDetailUrl", taskDetailUrl);
								data.put("changedProperties", changedProperties);

								sendMailObject.setData(data);

								Locale locale = getLocaleResolver()
										.resolveLocale(request);
								if (Locales.VI.equals(locale.toString())) {
									sendMailObject
											.setTemplate("vi_templates/updateTask.vm");
								} else if (Locales.EN.equals(locale.toString())) {
									sendMailObject
											.setTemplate("en_templates/updateTask.vm");
								}

								Employee reporter = employeeService
										.findByVisa(visa);
								List<String> bcc = new ArrayList<String>();

								if (!task.getAssignedto().isEmpty()) {
									Employee assignedTo = employeeService
											.findByVisa(task.getAssignedto());
									sendMailObject.setTo(assignedTo.getEmail());
									bcc.add(reporter.getEmail());
								} else {
									sendMailObject.setTo(reporter.getEmail());
								}

								if (!task.getMonitoredby().isEmpty()) {
									Employee monitoredBy = employeeService
											.findByVisa(task.getMonitoredby());
									bcc.add(monitoredBy.getEmail());
								}

								sendMailObject.setBcc(bcc);
								sendMailObject.setAttachedFiles(attachments);

								ActivityLog activityLog = new ActivityLog();
								activityLog
										.setProjectId(project.getProjectid());
								activityLog.setSubjectId(informTask.getCode());
								activityLog.setSubjectType("Task");
								activityLog.setCreatedAt(now);
								activityLog.setCreatedBy(visa);
								activityLog.setSubjectName(subject.toString());
								activityLog.setMessage("Update");
								activityLogService.save(activityLog);

								sendMailService.send(sendMailObject);
								log.info("UpdateTaskController || send mail done");
							}
							informTask.setVersion(version + 1);
							request.setAttribute("editOK", true);
						}

					} else {
						request.setAttribute("errorCode", errorCode);
					}
				}
			}
		}
		return showForm(request, response, errors);
	}

	@Override
	protected ModelAndView showForm(HttpServletRequest request,
			HttpServletResponse response, BindException errors)
			throws Exception {
		log.info("UpdateTaskController || showForm");
		Map<String, Object> bean = new HashMap<String, Object>();
		if (userService.isSignedIn(request)) {
			String loginName = userService.getCurrentLogin(request);
			bean.put("user", userService.findByLoginName(loginName));
			bean.put("isSigned", true);
			bean.put("isAdmin", userService.isAdmin(loginName));
			bean.put(Constants.LOGIN_NAME, loginName);
			bean.put(Constants.USER_NAME, userService.getUsername(loginName));
			String visa = userService.getVisa(loginName);
			bean.put("visa", visa);

			int pnum = ParamUtil.getInteger(request, "pnum", 0);
			Project project = projectService.findByNumber(pnum);
			bean.put("project", project);

			bean.put("severities", taskSeverityService.getAll());
			bean.put("statuses", taskStatusService.findByIdList(TaskStatusUtil
					.getDependencyStatusesForHiding((int) TaskStatuses.NEW)));
			bean.put("categories", taskCategoryService.getAll());
			bean.put("priorities", taskPriorityService.getAll());
			bean.put("phases", phaseService.getAll());

			if (project != null) {
				bean.put("efips", employeeFunctionInProjectService
						.findByProjectId(project.getProjectid()));
			}

			String returnToFullPageURL = ParamUtil.getString(request,
					Constants.RETURN_TO_FULL_PAGE_URL, "");
			if (returnToFullPageURL.isEmpty()) {
				returnToFullPageURL = request.getHeader("referer");
			}
			bean.put(Constants.RETURN_TO_FULL_PAGE_URL, returnToFullPageURL);

			request.setAttribute("bean", bean);
		} else {
			int pnum = ParamUtil.getInteger(request, "pnum", 0);
			String tcode = ParamUtil.getString(request, "tcode", "");
			String redirectLink = UrlConstants.UPDATE_TASK_URL
					+ UrlConstants.HTM + "?pnum=" + pnum + "&tcode=" + tcode;
			request.getSession().setAttribute("redirectLink", redirectLink);
			response.sendRedirect(UrlConstants.HOME_PAGE_URL + UrlConstants.HTM);
		}
		return super.showForm(request, response, errors);
	}

	private List<TranslatorObject> getChangedProperties(
			HttpServletRequest request, Task task0, Task task) {
		List<TranslatorObject> translations = new ArrayList<TranslatorObject>();
		Locale locale = getLocaleResolver().resolveLocale(request);

		if (!task0.getTaskCategory().getCategoryid()
				.equals(task.getTaskCategory().getCategoryid())) {
			String value = messageSource.getMessage("Category", null, locale);
			String description = task0.getTaskCategory().getName()
					+ " --> "
					+ taskCategoryService.findById(
							task.getTaskCategory().getCategoryid()).getName();
			TranslatorObject translatorObject = new TranslatorObject(
					locale.toString(), "Category", value, description);
			translations.add(translatorObject);
		}

		if (!task0.getTaskStatus().getStatusid()
				.equals(task.getTaskStatus().getStatusid())) {
			String value = messageSource.getMessage("Status", null, locale);
			String description = task0.getTaskStatus().getName()
					+ " --> "
					+ taskStatusService.findById(
							task.getTaskStatus().getStatusid()).getName();
			TranslatorObject translatorObject = new TranslatorObject(
					locale.toString(), "Status", value, description);
			translations.add(translatorObject);
		}

		if (!task0.getTaskSeverity().getSeverityid()
				.equals(task.getTaskSeverity().getSeverityid())) {
			String value = messageSource.getMessage("Severity", null, locale);
			String description = task0.getTaskSeverity().getName()
					+ " --> "
					+ taskSeverityService.findById(
							task.getTaskSeverity().getSeverityid()).getName();
			TranslatorObject translatorObject = new TranslatorObject(
					locale.toString(), "Severity", value, description);
			translations.add(translatorObject);
		}

		if (!task0.getTaskPriority().getPriorityid()
				.equals(task.getTaskPriority().getPriorityid())) {
			String value = messageSource.getMessage("Priority", null, locale);
			String description = task0.getTaskPriority().getName()
					+ " --> "
					+ taskPriorityService.findById(
							task.getTaskPriority().getPriorityid()).getName();
			TranslatorObject translatorObject = new TranslatorObject(
					locale.toString(), "Priority", value, description);
			translations.add(translatorObject);
		}

		if (task0.getActualtime() == null) {
			if (task.getActualtime() != null) {
				String value = messageSource.getMessage("Actual.hours", null,
						locale);
				String description = "undefined --> " + task.getActualtime();
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Actual.hours", value, description);
				translations.add(translatorObject);
			}
		} else {
			if (task.getActualtime() == null) {
				String value = messageSource.getMessage("Actual.hours", null,
						locale);
				String description = task0.getActualtime() + " --> undefined";
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Actual.hours", value, description);
				translations.add(translatorObject);
			} else if (!task0.getActualtime().equals(task.getActualtime())) {
				String value = messageSource.getMessage("Actual.hours", null,
						locale);
				String description = task0.getActualtime() + " --> "
						+ task.getActualtime();
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Actual.hours", value, description);
				translations.add(translatorObject);
			}
		}

		if (task0.getEstimatetime() == null) {
			if (task.getEstimatetime() != null) {
				String value = messageSource.getMessage("Estimate.hours", null,
						locale);
				String description = "undefined --> " + task.getEstimatetime();
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Estimate.hours", value, description);
				translations.add(translatorObject);
			}
		} else {
			if (task.getEstimatetime() == null) {
				String value = messageSource.getMessage("Estimate.hours", null,
						locale);
				String description = task0.getEstimatetime() + " --> undefined";
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Estimate.hours", value, description);
				translations.add(translatorObject);
			} else if (!task0.getEstimatetime().equals(task.getEstimatetime())) {
				String value = messageSource.getMessage("Estimate.hours", null,
						locale);
				String description = task0.getEstimatetime() + " --> "
						+ task.getEstimatetime();
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Estimate.hours", value, description);
				translations.add(translatorObject);
			}
		}

		if (DateTimeUtil.compare2Days(task0.getStarttime(),
				task.getStarttime(), "yyyyMMdd") != 0) {
			String value = messageSource.getMessage("Start.date", null, locale);
			String description = DateTimeUtil.dateToString(
					task0.getStarttime(), "yyyy-MM-dd")
					+ " --> "
					+ DateTimeUtil.dateToString(task.getStarttime(),
							"yyyy-MM-dd");
			TranslatorObject translatorObject = new TranslatorObject(
					locale.toString(), "Start.date", value, description);
			translations.add(translatorObject);
		}

		if (task0.getEndtime() == null) {
			if (task.getEndtime() != null) {
				String value = messageSource.getMessage("End.date", null,
						locale);
				String description = "undefined --> "
						+ DateTimeUtil.dateToString(task.getEndtime(),
								"yyyy-MM-dd");
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "End.date", value, description);
				translations.add(translatorObject);
			}
		} else {
			if (task.getEndtime() == null) {
				String value = messageSource.getMessage("End.date", null,
						locale);
				String description = DateTimeUtil.dateToString(
						task0.getEndtime(), "yyyy-MM-dd")
						+ " --> undefined";
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "End.date", value, description);
				translations.add(translatorObject);
			} else if (DateTimeUtil.compare2Days(task0.getEndtime(),
					task.getEndtime(), "yyyyMMdd") != 0) {
				String value = messageSource.getMessage("End.date", null,
						locale);
				String description = DateTimeUtil.dateToString(
						task0.getEndtime(), "yyyy-MM-dd")
						+ "-->"
						+ DateTimeUtil.dateToString(task.getEndtime(),
								"yyyy-MM-dd");
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "End.date", value, description);
				translations.add(translatorObject);
			}
		}

		if (task0.getAssignedto().isEmpty()) {
			if (!task.getAssignedto().isEmpty()) {
				String value = messageSource.getMessage("Assigned.to", null,
						locale);
				String description = "undefined --> " + task.getAssignedto();
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Assigned.to", value, description);
				translations.add(translatorObject);
			}
		} else {
			if (task.getAssignedto().isEmpty()) {
				String value = messageSource.getMessage("Assigned.to", null,
						locale);
				String description = task0.getAssignedto() + " --> undefined";
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Assigned.to", value, description);
				translations.add(translatorObject);
			} else if (!task.getAssignedto().equals(task0.getAssignedto())) {
				String value = messageSource.getMessage("Assigned.to", null,
						locale);
				String description = task0.getAssignedto() + " --> "
						+ task.getAssignedto();
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Assigned.to", value, description);
				translations.add(translatorObject);
			}
		}

		if (task0.getMonitoredby().isEmpty()) {
			if (!task.getMonitoredby().isEmpty()) {
				String value = messageSource.getMessage("Monitored.by", null,
						locale);
				String description = "undefined --> " + task.getMonitoredby();
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Monitored.by", value, description);
				translations.add(translatorObject);
			}
		} else {
			if (task.getMonitoredby().isEmpty()) {
				String value = messageSource.getMessage("Monitored.by", null,
						locale);
				String description = task0.getMonitoredby() + " --> undefined";
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Monitored.by", value, description);
				translations.add(translatorObject);
			} else if (!task.getMonitoredby().equals(task0.getMonitoredby())) {
				String value = messageSource.getMessage("Monitored.by", null,
						locale);
				String description = task0.getMonitoredby() + " --> "
						+ task.getMonitoredby();
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Monitored.by", value, description);
				translations.add(translatorObject);
			}
		}

		if (task0.getPhase().isEmpty()) {
			if (!task.getPhase().isEmpty()) {
				String value = messageSource.getMessage("Phase", null, locale);
				String description = "undefined --> " + task.getPhase();
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Phase", value, description);
				translations.add(translatorObject);
			}
		} else {
			if (task.getPhase().isEmpty()) {
				String value = messageSource.getMessage("Phase", null, locale);
				String description = task0.getPhase() + " --> undefined";
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Phase", value, description);
				translations.add(translatorObject);
			} else if (!task.getPhase().equals(task0.getPhase())) {
				String value = messageSource.getMessage("Phase", null, locale);
				String description = task0.getPhase() + " --> "
						+ task.getPhase();
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "Phase", value, description);
				translations.add(translatorObject);
			}
		}

		if (task0.getPercentcomplete() == null) {
			if (task.getPercentcomplete() != null) {
				String value = messageSource.getMessage("%completed", null,
						locale);
				String description = "0% --> " + task.getPercentcomplete()
						+ "%";
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "%completed", value, description);
				translations.add(translatorObject);
			}
		} else {
			if (task.getPercentcomplete() == null) {
				String value = messageSource.getMessage("%completed", null,
						locale);
				String description = task0.getPercentcomplete() + "% --> 0%";
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "%completed", value, description);
				translations.add(translatorObject);
			} else if (!task0.getPercentcomplete().equals(
					task.getPercentcomplete())) {
				String value = messageSource.getMessage("%completed", null,
						locale);
				String description = task0.getPercentcomplete() + "% --> "
						+ task.getPercentcomplete() + "%";
				TranslatorObject translatorObject = new TranslatorObject(
						locale.toString(), "%completed", value, description);
				translations.add(translatorObject);
			}
		}
		return translations;
	}

	private boolean checkChild(Task task, long status) {
		if (task.getTasksForTaskchildid() != null) {
			if (task.getTasksForTaskchildid().size() == 0) {
				return true;
			} else if (task.getTasksForTaskchildid().size() > 0) {
				Set<Task> childTask = task.getTasksForTaskchildid();
				for (Task child : childTask) {
					if (child.getTaskStatus().getStatusid() < status) {
						return false;
					}
				}
			}
		}
		return true;
	}

	private boolean checkChildRecursion(Task task, long status) {
		boolean flag = checkChild(task, status);
		if (!flag) {
			return flag;
		}
		Set<Task> childTask = task.getTasksForTaskchildid();
		for (Task child : childTask) {
			if (child.getTaskStatus().getStatusid() < status) {
				return false;
			} else {
				checkChildRecursion(child, status);
			}
		}
		return flag;
	}

	private boolean checkParent(Task task, long status) {
		if (task.getTasksForTaskid() != null) {
			if (task.getTasksForTaskid().size() == 0) {
				return true;
			} else if (task.getTasksForTaskid().size() == 1) {
				Set<Task> parentTask = task.getTasksForTaskid();
				for (Task parent : parentTask) {
					if (parent.getTaskStatus().getStatusid() > status) {
						return false;
					}
				}
			}
		}
		return true;
	}

	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public TaskSeverityService getTaskSeverityService() {
		return taskSeverityService;
	}

	public void setTaskSeverityService(TaskSeverityService taskSeverityService) {
		this.taskSeverityService = taskSeverityService;
	}

	public TaskCategoryService getTaskCategoryService() {
		return taskCategoryService;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public TaskStatusService getTaskStatusService() {
		return taskStatusService;
	}

	public void setTaskStatusService(TaskStatusService taskStatusService) {
		this.taskStatusService = taskStatusService;
	}

	public void setTaskCategoryService(TaskCategoryService taskCategoryService) {
		this.taskCategoryService = taskCategoryService;
	}

	public TaskPriorityService getTaskPriorityService() {
		return taskPriorityService;
	}

	public void setTaskPriorityService(TaskPriorityService taskPriorityService) {
		this.taskPriorityService = taskPriorityService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public EmployeeFunctionInProjectService getEmployeeFunctionInProjectService() {
		return employeeFunctionInProjectService;
	}

	public void setEmployeeFunctionInProjectService(
			EmployeeFunctionInProjectService employeeFunctionInProjectService) {
		this.employeeFunctionInProjectService = employeeFunctionInProjectService;
	}

	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public TaskLogService getTaskLogService() {
		return taskLogService;
	}

	public void setTaskLogService(TaskLogService taskLogService) {
		this.taskLogService = taskLogService;
	}

	public PhaseService getPhaseService() {
		return phaseService;
	}

	public void setPhaseService(PhaseService phaseService) {
		this.phaseService = phaseService;
	}

	public SendMailService getSendMailService() {
		return sendMailService;
	}

	public void setSendMailService(SendMailService sendMailService) {
		this.sendMailService = sendMailService;
	}

	public LocaleResolver getLocaleResolver() {
		return localeResolver;
	}

	public void setLocaleResolver(LocaleResolver localeResolver) {
		this.localeResolver = localeResolver;
	}

	public ReloadableResourceBundleMessageSource getMessageSource() {
		return messageSource;
	}

	public void setMessageSource(
			ReloadableResourceBundleMessageSource messageSource) {
		this.messageSource = messageSource;
	}

	public ActivityLogService getActivityLogService() {
		return activityLogService;
	}

	public void setActivityLogService(ActivityLogService activityLogService) {
		this.activityLogService = activityLogService;
	}

}
